//
//  RecommendCell.m
//  KTVShopProduct
//
//  Created by paeanSs on 2017/11/8.
//  Copyright © 2017年 paeanSs. All rights reserved.
//

#import "RecommendCell.h"

#define Position_Icon @"Res_icon_position"
#define Gift_Icon @"Res_icon_Promotions"

@implementation RecommendCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupSubViews];
    }
    return self;
}

- (void)setupSubViews{
    CGFloat logoWidth = 75;
    CGFloat logoLeft = 15;
    
    self.backgroundColor = [UIColor clearColor];
    
    self.sepLbl = [[UILabel alloc] init];
    self.sepLbl.backgroundColor = UIColorFromHex(0xebebeb);
    [self.contentView addSubview:self.sepLbl];
    
    [self.sepLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(KTScreenWidth - logoLeft *2, 1));
        make.top.centerX.equalTo(self);
    }];

    self.logoView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img_platform_home_store_small"]];
    [self.contentView addSubview:self.logoView];
    
    [self.logoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(logoWidth, logoWidth));
        make.left.equalTo(self).offset(logoLeft);
        make.centerY.equalTo(self);
    }];
    
    CGFloat lblLeft = 12;
    CGFloat lblTop = 5;
    
    self.titleLbl = [[UILabel alloc] init];
    self.titleLbl.text = @"好乐迪(闵行9馆店)";
    self.titleLbl.textColor = UIColorFromHex(0x232323);
    self.titleLbl.font = [UIFont systemFontOfSize:15];
    self.titleLbl.textAlignment = NSTextAlignmentLeft;
    [self.titleLbl sizeToFit];
    [self.contentView addSubview:self.titleLbl];
    
    [self.titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(self.titleLbl);
        make.top.equalTo(self.logoView);
        make.left.equalTo(self.logoView.mas_right).offset(lblLeft);
    }];
    
    self.salesLbl = [[UILabel alloc] init];
    self.salesLbl.text = @"销量：3000单";
    self.salesLbl.textColor = NormalDefalutColor;
    self.salesLbl.font = [UIFont systemFontOfSize:12];
    self.salesLbl.textAlignment = NSTextAlignmentLeft;
    [self.salesLbl sizeToFit];
    [self.contentView addSubview:self.salesLbl];
    [self.salesLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(self.salesLbl);
        make.left.equalTo(self.titleLbl);
        make.top.equalTo(self.titleLbl.mas_bottom).offset(lblTop);
    }];
    
    self.distanceLbl = [[UILabel alloc] init];
    self.distanceLbl.text = @"  距离1.3km";
    self.distanceLbl.textColor = NormalDefalutColor;
    self.distanceLbl.font = [UIFont systemFontOfSize:12];
    self.distanceLbl.textAlignment = NSTextAlignmentLeft;
    [self.distanceLbl sizeToFit];
    [self.contentView addSubview:self.distanceLbl];
    self.distanceLbl.attributedText = [self setLabelText:self.distanceLbl.text withIcon:Position_Icon titleColor:NormalDefalutColor];
    [self.distanceLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(self.distanceLbl);
        make.left.equalTo(self.titleLbl);
        make.top.equalTo(self.salesLbl.mas_bottom).offset(lblTop);
    }];
    
    self.activityLbl = [[UILabel alloc] init];
    self.activityLbl.text = @" 酒水满一送一活动";
    self.activityLbl.textColor = SelectDefalutColor;
    self.activityLbl.font = [UIFont systemFontOfSize:12];
    self.activityLbl.textAlignment = NSTextAlignmentLeft;
    [self.activityLbl sizeToFit];
    [self.contentView addSubview:self.activityLbl];
    self.activityLbl.attributedText = [self setLabelText:self.activityLbl.text withIcon:Gift_Icon titleColor:SelectDefalutColor];
    [self.activityLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(self.activityLbl);
        make.left.equalTo(self.titleLbl);
        make.bottom.equalTo(self.logoView);
    }];
    self.starView = [[KTStarRateView alloc] initWithFrame:CGRectMake(200, 50, 100, 20)];
    
    [self.contentView addSubview:self.starView];
    
    [self.starView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 20));
        make.right.equalTo(self).offset(-lblLeft);
        make.bottom.equalTo(self.logoView);
    }];
}


- (NSMutableAttributedString *)setLabelText:(NSString *)text withIcon:(NSString *)icon titleColor:(UIColor *)titleColor{
    
    NSTextAttachment *attch = [[NSTextAttachment alloc] init];
    attch.image = [UIImage imageNamed:icon];
    attch.bounds = CGRectMake(0, -2, attch.image.size.width, attch.image.size.height);
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString attributedStringWithAttachment:attch] mutableCopy];
    NSDictionary *attrDict= @{NSForegroundColorAttributeName:titleColor,NSFontAttributeName:[UIFont systemFontOfSize:12]};
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",text] attributes:attrDict];
    [attrStr appendAttributedString:attri];
    
    return attrStr;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
